const authCodeModel = require('../mongoModel/authCodeModel')
const pcuserModel = require('../mongoModel/pcUserModel')
const emailPassModle = require('../mongoModel/emailerPassModel')
var moment = require('moment')
const service = {
  addCode (code) {
    return authCodeModel.create({
      createTime: Date.now(),
      createAt: moment().add(5, 'minutes').toDate(),
      code,
    })
  },
  findEmailer (email, password) {
    return pcuserModel.find({ email, password })
  },
  // 查找验证码
  Register (code) {
    return authCodeModel.find({ code })
  },
  // 前台注册用户
  addUser (email, username, password, avatar) {
    return pcuserModel.create({
      createTime: Date.now(),
      email,
      username,
      password,
      avatar,
    })
  },
  updateUser (email, where) {
    return pcuserModel.updateOne({ email }, where)
  },
  // 修改邮箱授权码
  async updateEmailpass (id, emailpass) {
    let data = await emailPassModle.find({ id }).catch((err) => err)
    if (data.length) {
      return emailPassModle
        .updateOne(
          { id },
          {
            emailpass,
          }
        )
        .catch((err) => err)
    } else {
      return emailPassModle
        .create({
          id,
          emailpass,
        })
        .catch((err) => err)
    }
  },
  // 查找邮箱授权码
  getEmailPass (id) {
    return emailPassModle.find({ id }).catch((err) => err)
  },
  // 查找
  async findUser (page, limite, where) {
    let count = await pcuserModel.find(where).count()
    let data = await pcuserModel
      .find(where)
      .skip((page - 1) * limite)
      .sort({ createTime: -1 })
      .limit(limite)
    return { count, data }
  },
  // 删除单个
  delUserSingle (_id) {
    return pcuserModel.deleteOne({ _id })
  },
  // 删除多个用户
  delBatch (delBatchItem) {
    return pcuserModel.deleteMany({
      _id: {
        $in: delBatchItem,
      },
    })
  },
  // 查找全部用户
  async endfinAll () {
    return { user: await pcuserModel.find().count() }
  }
}
module.exports = service
